(function($){
   $.fn.imagePromo = function(options) {
      
      var container = this;

      options = $.extend({
         interval: 5000, //switching interval in milliseconds
         transition: 1500 //transition (fadeIn|fadeOut) time
      }, options || {});


      function moveToCenter($img, bHide) {
         if (!$img.is('img') || !$img.width() || !$img.height() || !$img.parent().width() || !$img.parent().height())
             return false;
         
         
         // place the image on the center and crop it (if neccessary) using parent
         $img.parent().css({
             overflow: 'hidden',
             position: 'relative'
         });
        
         //77youyue: don't offset the position    
         $img.css({
             position: 'absolute',
             left: 0, //Math.round( ($img.parent().width()  / 2) - ($img.width()  / 2)),
             top:  0 //Math.round( ($img.parent().height() / 2) - ($img.height() / 2))
         });
         
         if (bHide)
             $img.hide();
      }
       
      function switchNextImg() {
         var $curImg  = $('img:visible', container);
         var $nextImg = $curImg.next();
         
         if (!$nextImg.length)
             var $nextImg = $('img:first', container);
         
         $curImg.fadeOut(options.transition);
         $nextImg.fadeIn(options.transition);

         if (options.altBox) {
            $(options.altBox).fadeOut('fast', function() {
               $(options.altBox).fadeIn(options.transition).html($nextImg.attr("alt"));
            })
         } 

         if (options.titleBox) {
            $(options.titleBox).fadeOut('fast', function() {
               $(options.titleBox).fadeIn(options.transition).html($nextImg.attr("title"));
            })
         }
      }
       
      $('img', container).each(function(i) {
         
         var $img = $(this);
         var bHide = (i > 0); // do not hide only first
         
         if($img.width() && $img.height()) {
             moveToCenter($img, bHide);
         } else {
             // attach event on load
             if( document.all ) { //ie
   				$img.ready(function(){
                   moveToCenter($img, bHide);
                 });
   			} else {
   				$img.load(function(){
                   moveToCenter($img, bHide);
                 });
             }
         }
      });
      
 
      // begin switching
      setInterval(function(){
         switchNextImg();
      }, options.interval);
   };
})(jQuery);
